<template>
	<view>
		<view class="page">
			<comp-common v-bind="{data:data}" @getUserInfo="getUserInfo" @myLogin="myLogin" @getPhoneNumber="getPhoneNumber" @hideGetCoupon="hideGetCoupon" @closeCouponBox="closeCouponBox" @navigatorClick="navigatorClick"></comp-common>
			<comp-header v-bind="{data:data}" @relevanceError="relevanceError" @relevanceSuccess="relevanceSuccess"></comp-header>
			<view class="body after-navber">
				<view>
            <block v-if="data.is_bind===1">
                <view class="flex-row flex-x-center">
                    <image class="bind_yes_img" :src="data.app.app_logo?data.app.app_logo:data.__wxapp_img.dapp.binding_yes.url"></image>
                </view>
                <view class="flex-row flex-x-center title">
                    <text>{{data.app.title}}</text>
                </view>
            </block>
            <block v-if="data.is_bind===2">
                <view class="flex-row flex-x-center img_box">
                    <image class="head_img" :src="data.user.avatar_url"></image>
                    <view class="img_binding_box">
                        <image class="img_binding" :src="data.__wxapp_img.dapp.binding.url"></image>
                    </view>
                    <image class="head_img" :src="data.app.app_logo?data.app.app_logo:data.__wxapp_img.dapp.binding_yes.url"></image>
                </view>
                <view class="flex-row flex-x-center title">
                    <text>{{data.app.title}}</text>
                </view>
                <view class="flex-row flex-x-center content">
                    <text>及时接收消息推送</text>
                </view>
                <button bindgetuserinfo="getUserInfo" class="flex-row flex-x-center auth-info" openType="getUserInfo">绑定公众号</button>
            </block>
        </view>
			</view>
			<comp-footer v-bind="{data:data}"></comp-footer>
		</view>
	</view>
</template>

<script>var myVue = {};
	export default {
		data() {
			return {
				data: {
        user: {},
        is_bind: "",
        app: {}
    },
			};
		},
		onLoad: function(e) {myVue = this;
        myVue.getApp.page.onLoad(this, e), this.checkBind();
        var t = myVue.getApp.core.getStorageSync(myVue.getApp.const.USER_INFO);
        this.setData({
            user: t
        });
    },
		onReady: function(e) {
        myVue.getApp.page.onReady(this);
    },
    onShow: function(e) {
        myVue.getApp.page.onShow(this);
    },
    onHide: function(e) {
        myVue.getApp.page.onHide(this);
    },
    onUnload: function(e) {
        myVue.getApp.page.onUnload(this);
    },
    onPullDownRefresh: function(e) {
        myVue.getApp.page.onPullDownRefresh(this);
    },
    onReachBottom: function(e) {
        myVue.getApp.page.onReachBottom(this);
    },
		methods: {
			checkBind: function() {
        var e = this;
        myVue.getApp.core.showLoading({
            title: "加载中"
        }), myVue.getApp.request({
            url: myVue.getApp.api.user.check_bind,
            success: function(t) {
                e.getApp.core.hideLoading(), 0 === t.code && e.setData({
                    is_bind: t.data.is_bind,
                    app: t.data.app
                });
            }
        });
    },
    getUserInfo: function(e) {
        myVue.getApp.core.showLoading({
            title: "加载中"
        });
        var t = this;
        myVue.getApp.core.login({
            success: function(o) {
                var n = o.code;
                myVue.getApp.request({
                    url: myVue.getApp.api.passport.login,
                    method: "POST",
                    data: {
                        code: n,
                        user_info: e.detail.rawData,
                        encrypted_data: e.detail.encryptedData,
                        iv: e.detail.iv,
                        signature: e.detail.signature
                    },
                    success: function(e) {
                        t.getApp.core.hideLoading(), 0 === e.code ? (t.getApp.core.showToast({
                            title: "登录成功,请稍等...",
                            icon: "none"
                        }), t.bind()) : t.getApp.core.showToast({
                            title: "服务器出错，请再次点击绑定",
                            icon: "none"
                        });
                    }
                });
            }
        });
    },
    bind: function() {
			var _this = this;
        myVue.getApp.request({
            url: myVue.getApp.api.user.authorization_bind,
            data: {},
            success: function(e) {
                if (0 === e.code) {
                    var t = encodeURIComponent(e.data.bind_url);
                    myVue.getApp.core.redirectTo({
                        url: "/pages/web/web?url=" + t
                    });
                } else myVue.getApp.core.showToast({
                    title: e.msg,
                    icon: "none"
                });
            }
        });
    },
		},
	}
</script>

<style scoped>
.img_box {
    margin: 80upx 0 0 40upx;
}

.img_binding_box {
    height: 120upx;
    line-height: 150upx;
}

.img_binding {
    width: 50upx;
    height: 50upx;
    margin: 0 32upx;
}

.head_img {
    width: 120upx;
    height: 120upx;
    border-radius: 16upx;
}

.title {
    margin-top: 40upx;
    font-size: 11pt;
    color: #3f3f3f;
}

.content {
    margin-top: 20upx;
    font-size: 9pt;
    color: #999999;
}

.auth-info {
    margin: 64upx 24upx 0;
    width: 702upx;
    height: 88upx;
    line-height: 88upx;
    background-color: #ff4544;
    border-radius: 44upx;
    color: #ffffff;
    font-size: 11pt;
}

.bind_yes_img {
    width: 300upx;
    height: 300upx;
    margin: 120upx 0 64upx;
    border-radius: 44upx;
}
</style>
